
//获取出院科室
$("#audit_dept").combobox({
	url:ctx + "/MedicalShootManage/getDept",
	valueField:"ftykh",
	textField:"fksname",
	width:120, 
	height:24,
	onLoadSuccess : function() {
		$('#audit_dept').combobox('select', 'TZYQY');
	}
});

$(function(){
//加载数据表格
	$("#Audit_Grid").datagrid({
		url:'',
		toolbar:"#auditBar",
		rownumbers:true,   //行号列
		fitColumns:false, //适应网格的宽度，防止水平滚动
		border : false,
		striped : true,
		fit : true,
		singleSelect: true,  //只允许选择一行 
		pagination : true,  //DataGrid控件底部显示分页工具栏
		pageSize : 20,
		pageList : [ 20, 30,50 ],
		columns:[[
		          /*{field:'',title:'',checkbox:true,align:'center'},*/
				{field:'fprn',title:'病案号',width:120,align:'center'},
				{field:'fstatus',title:'审核状态',width:80,align:'center',
					formatter:function(value){
						if(value == '0'){
							return "未审核";
						}else if(value == '1'){
							return "审核通过";
						}else if(value =='2'){
							return '审核未通过';
						}
					}
				},  
		        {field:'fname',title:'姓名',width:80,align:'center' },  
		        {field:'ftimes',title:'次数',width:50,align:'center' },  
		        {field:'fzyid',title:'住院ID',width:100,align:'center' },  
		        {field:'fsex',title:'性别',width:50,align:'center' },  
		        {field:'fbirthday',title:'出生日期',width:100,align:'center',
		        	formatter:function(value,row,index){
		        		return new Date(value).format("yyyy-MM-dd");
		        	}
		        },  
		        {field:'fbarcode',title:'条码号',width:160,align:'center' },  
		        {field:'fcydate',title:'出院日期',width:100,align:'center',
		        	formatter:function(value,row,index){
		        		return  new Date(value).format("yyyy-MM-dd");
		        	}
		        }, 
		        {field:'frydept',title:'入院科室',width:100,align:'center' },  
		        {field:'fcydept',title:'出院科室',width:100,align:'center' },  
		        {field:'frydate',title:'入院日期',width:100,align:'center',hidden:true,
		        	formatter:function(value){
		        		return new Date(value).format("yyyy-MM-dd");
		        	}	
		        }  
		       ]]
	});
	
	//根据权限设置判断页面按钮是否禁用
    var tab = $("#tabs").tabs("getSelected");  //获取当前选择的面板
	var tabtext = tab.panel("options").title;  //获取当前选择面板的标题
	
	$.ajax({
		url : ctx + "/MedicalShootManage/getBtnPermision",
		dataType : 'json',
		type : 'post',
		data : {tabtext : tabtext},
		success : function(data){
			var zjcount = 0;
			if (data.length > 0) {
				for (var i = 0; i < data.length; i++) {
					if (data[i].FCONTENTRIGHT.charAt(0) == "1") {
						zjcount ++ ;
						break;
					}
				}
			}
			//判断该页面如果所有的权限设置质检按钮禁用的话执行的操作
			if (zjcount == 0) {
				$('#auditBtn').linkbutton('disable');
			}
		}
	})
	
});

//加载表格数据
function AuditGrid(){
	var auditDept = $("#audit_dept").combobox("getValue");
	var auditDate1 = $("#audit_date1").datebox("getValue");
	var auditDate2 = $("#audit_date2").datebox("getValue");
	var auditName = $("#audit_name").textbox("getValue");
	var auditFprn = $("#audit_fprn").textbox("getValue");
	var mode = $("#audit_mode").combobox("getValue");
	$("#Audit_Grid").datagrid({
		url:ctx + "/MedicalShootManage/getAuditData",
		queryParams:{
			"auditDept": auditDept,
			"auditDate1": auditDate1,
			"auditDate2": auditDate2,
			"auditName": auditName,
			"auditFprn": auditFprn,
			"mode":mode 
		}
	});
}



//质检
function AuditCheck(){
	var items = $("#Audit_Grid").datagrid("getSelected");
	if(items =="" || items ==null){
		alert("请选中数据！");
		return;
	}
	var fprn = items.fprn;
	var ftimes = items.ftimes;
	var fname = items.fname;
	var fbarcode = items.fbarcode;
	var fcydate = new Date(items.fcydate).format("yyyy-MM-dd");
	$('#auditTabs').tabs('select', '病案质检');
	//获取树节点
	$("#auditTree").tree({    
	    url: ctx + "/MedicalShootManage/tree",  
	    animate: true,
	    checkbox:false,
	    queryParams:{
	    	"fprn":fprn,
	    	"ftimes":ftimes,
	    	"fzyid":items.fzyid
	    },
	    onSelect:function(node){ 
	    	 $(".RecordA_bgSliderIMG").css("display",'none');
	    	 $(".RecordA_SliderIMG").css("display",'none');
	    	    
			if(node.text != "病案封面"){
		    	//点击分为两种情况：情景一，直接点击图片;情景二：点击二级菜单;
				var id = node.id; 	//id绑定了图片编号
				var imgNumber = id.substring(id.lastIndexOf(",")+1,id.length);	//拆分图片编号
		    	if(!node.children){  	//情景一
		    		var src = node.attributes;
		    		var isMore = false;
		    		$.ajax({
	    				url: ctx + "/MedicalShootManage/encodeIMG",
		 	    		dataType : 'json',
		 	    		type : 'post',
		 	    		data:{
		 	    			"path" : src,
		 	    			"isMore" : isMore
		 	    		},
		 	    		success:function(data){
		 	    			$("#RecordSlider").siblings("a").remove(); //先清空插件的元素
		 	    			$("#RecordSlider").html("");  //清空
		 	    			
		 	    			if(src.substring(src.lastIndexOf(".")+1,src.length) == "jpg"){
		 		    			var imgName = "";
		 		    			//var node = $("#auditTree").tree("getNode",node.target);
		 		    			var parent = $("#auditTree").tree("getParent",node.target);
		 		    			var pageNumber = node.text;
		 		    			var path = node.attributes;
		 		    			var parentText = parent.text;//父类标题
		 		    			var cateNumber = parent.attributes;//图片分类编码
		 		    			var src2 = src.replace(/\\/g,'/');
		 		    			var index = src2.lastIndexOf("/");
		 		    			var imgSrc = src2.substring(index+1,src.length);
		 		    			
		 		    			var srcPath=ctx + "/upload/"+fcode+"/"+imgSrc; 
		 		    			$("#RecordSlider").html( 
		 		    					 "<div class='wrapper'>"
		 		    				         +"<div id='RecordA_Viewer' class='viewer iviewer_cursor' style='height:94%;overflow: scroll;'></div><br/>"
		 		    				    +"</div>"
		 		    				 	+ "<div class='caption'>" +
		 		    						"<span class='PageNumber'>"+pageNumber+"&nbsp;"+parentText+"&nbsp;"+imgSrc+"</span>" +
		 		    						"<span class='auditImgNumber' style='display:none;'>"+imgNumber+"</span>" +
		 		    						"<span class='imgCategory' style='display:none;'>"+cateNumber+"</span>" +
		 		    						"<span class='imgPath' style='display:none;'>" + path + "</span>" +
		 		    						"<span class='pageCode' style='display:none;'>" + pageNumber + "</span>" +
		 		    					"</div>" );  
		 		    				    $("#RecordSlider").removeClass("slide");
		 		    				    
		 		    				    RecordA_Viewer("RecordA_Viewer",srcPath);
		 		    				   
		 		    				 	if(!node.children){ 
		 		    						$(".slide_nav").css("display",'none')
		 		    					}   
		 		    		}else{
		 		    			return ;
		 		    		}
		 	    		},
		 	    		error:function(){
		 	    			$.messager.alert("提示","解密图片失败，请联系管理员！","info");
		 	    		}
	    				
	    			});
		    		
		    	}else{  //情景二
		    		putImg(node);
		    	}
			}else{
				return;
			}
		},
		onLoadSuccess:function(){
			
		}
	});
	//给病案质检页面隐藏的元素填充值
	$(".fprn").html(fprn);
	$(".ftimes").html(ftimes);
	$(".fname").html(fname);
	$(".fzyid").html(items.fzyid);
	$(".fcydate").html(fcydate);
	$(".barcode").html(fbarcode);
}
var fcode =$("#fcode").text(); //获取当前用户code

//放置图片至浏览区域
function putImg(node){
	var imgName = "";
	var html = "";
	//debugger;
	var parent = $("#auditTree").tree("getNode",node.target);
	var parentText = parent.text;//父类标题
	var cateNumber = parent.attributes;//图片分类编码
	var parentChildren = parent.children;//父类的子节点数组
	var path = "";
	var isMore = true; //为true，则解密多个
	for (var j = 0; j < parentChildren.length; j++) {
		var originPath = parentChildren[j].attributes; //图片路径
		if(j >0){
			path += ","+originPath;
		}else{
			path += originPath;
		}
	}
	$.ajax({
		url: ctx + "/MedicalShootManage/encodeIMG",
		dataType : "json",
		type : "post",
		data : {
			"path" :path,
			"isMore" : isMore
		},
		success:function(){
			 $(".RecordA_bgSliderIMG").css("display",'none');
	    	 $(".RecordA_SliderIMG").css("display",'none');
	    	 
			//循环获取子节点
			$("#RecordSlider").siblings("a").remove(); //先清空插件的元素
			for (var i = 0; i < parentChildren.length; i++) {
				var src = parentChildren[i].attributes; //图片路径
				var pageNumber = parentChildren[i].text;//图片页码
				var id = parentChildren[i].id; //图片编号
				//拆分图片编号
				var imgNumber = id.substring(id.lastIndexOf(",")+1,id.length);
				//截取图片路径
				var src2 = src.replace(/\\/g,'/');
				var index = src2.lastIndexOf("/");
				var imgSrc = src2.substring(index+1,src.length); 
				var srcPath=ctx + "/upload/"+fcode+"/"+imgSrc;
				html += "<li>"   
			        		//+"<div id='RecordViewer"+i+"'"+" class='viewer iviewer_cursor' style='height:99%;overflow:scroll;border:0px solid #000000;'></div><br/>"
							+ "<img src='"+ ctx + "/upload/"+fcode+"/"+imgSrc+"' onclick=imgClick('"+srcPath+"')>" 
						
							+"<div class='caption'>" +
								"<span class='PageNumber'>" + pageNumber + "&nbsp;" + parentText + "&nbsp;" + imgSrc + "</span>" +
								"<span class='auditImgNumber' style='display:none;'>" + imgNumber + "</span>" +
								"<span class='imgCategory' style='display:none;'>" + cateNumber + "</span>" +
								"<span class='imgPath' style='display:none;'>" + src + "</span>" +
								"<span class='pageCode' style='display:none;'>" + pageNumber + "</span>" +
							"</div>" +
						"</li>";   
			}
			$("#RecordSlider").html(html); 
			slider(); //渲染特效
		 
			/*//鼠标滚动图片方法 Ie8不兼容
			for (var i = 0; i < parentChildren.length; i++) { 
				var src = parentChildren[i].attributes; //图片路径 
				//截取图片路径
				var src2 = src.replace(/\\/g,'/');
				var index = src2.lastIndexOf("/");
				var imgSrc = src2.substring(index+1,src.length);
				
				var srcPath=ctx + "/upload/"+fcode+"/"+imgSrc;
				RecordA_Viewer("RecordViewer"+i,srcPath);
			} */
		},
		error:function(){
			$.messager.alert("提示","解密图片失败，请联系管理员！","info");
		}
	});
	
}

//关闭遮盖层
function closed(){   
    $(".RecordA_bgSliderIMG").css("display",'none');
    $(".RecordA_SliderIMG").css("display",'none');
}

//打开遮盖层
function imgClick(srcPath){ 
  //var srcPath = $("li").find('img').prop("src"); 
  $(".RecordA_bgSliderIMG").css("display",'block');
  $(".RecordA_SliderIMG").css("display",'block');
  $("#RecordA_SliderIMG").html("");   //先清空
  $("#RecordA_SliderIMG").html(
		    "<div class='wrapper'>"
          	+ "<div id='RecordA_SliderIMG_iviewer' class='viewer iviewer_cursor'></div><br/>"
            + "</div>" ); 
  
  RecordA_Viewer("RecordA_SliderIMG_iviewer",srcPath);
}
 
//鼠标滚动_图片放大效果
function RecordA_Viewer(iviewerId,srcPath){ 
	 /* 带有图片旋转功能的时候才用此*/
	 var $PA = jQuery; 
	 var viewer;
     viewer = $PA("#"+iviewerId).iviewer({
  	  src:srcPath
    });
    
     /*不带图片旋转的时候可以调用*/
	/* var $PA = jQuery; 
     var iviewer = {};
     $PA("#"+iviewerId).iviewer({
    	  src:srcPath,
    	  zoom: "fit",
	      zoom_min:30,
	      zoom_max: 400,
          initCallback: function()
          { 
            iviewer = this;
          } 
      }); */
}

//图片播放效果
function slider(){
  $("#RecordSlider").responsiveSlides({
	    auto: false,
	    pager: false,
	    nav: true,
	    speed: 500,
	    // 对应外层div的class : slide_container
	    namespace: "slide"
	    });
}

//关闭遮盖层
function AuditClosed(){
	document.getElementById("auditbg").style.display ="none";  
    document.getElementById("auditShow").style.display ="none";  
}

//审核失败
function fail(){
	$("#auditTree li:eq(0)>div:eq(0)").addClass("tree-node-selected");   //设置第一个节点高亮   
	$("#messageDialog").dialog({
        closed: false,
        modal: true,
		title: "消息提示",
		buttons:[{
			text:"确认", handler:function(){ 
				//审核通过
				//遍历整个tree，获取所有图片的编码和路径地址传入后台	
				var node = $("#auditTree").tree("getSelected");
				var oneArr = [];
				oneArr = node.children;
				var path = ""; //存放路径
			//	var numbers = ""; //存放图片编码
				for (var i = 0; i < oneArr.length; i++) {
					var secondary = oneArr[i].children;
					for (var j = 0; j < secondary.length; j++) {
						if(secondary.length > 0){
							path +=  secondary[j].attributes + ",";
						}
					}
				}
				//var path = $("#slider").find('li[class*=on]>div>.imgPath').text();//图片路径
				//var categoryNumber = $("#slider").find('li[class*=on]>div>.imgCategory').text();//图片分类编码
				//var number = $("#slider").find('li[class*=on]>div>.auditImgNumber').text();//图片编码
				$.ajax({
					url:ctx + "/MedicalShootManage/auditFail",
					dataType:'json',
					type:'post',
					data:{
					//	"id" : id,
						"path" : path,
					//	"categoryNumber" : categoryNumber,
						/*"fprn" : $(".AutidPrn").text(),//病案号
						"fname" : $(".AutidName").text(), //姓名
						"ftimes" : $(".AutidTimes").text(),	//次数
						"barcode":$(".barcode").text(),  //条形码
*/						"fzyid" : $(".AutidZyid").text()	//住院ID
					},
					success:function(data){
						if(data.msg == true){
							alert("操作成功！");
							$("#messageDialog").dialog("close");
							$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点   
						}else{
							alert("操作失败,请联系管理员！");
							$("#messageDialog").dialog("close");
							$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点
						}
					},
					error:function(){
						alert("操作失败,请联系管理员！");
						$("#messageDialog").dialog("close");
						$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点
					}
				});
		  }
		},{
			text:"关闭",
			handler:function(){
				$('#messageDialog').dialog("close");
			}
		}
		]
	});
	$(".AutidPrn").html($(".fprn").text());
	$(".AutidName").html($(".fname").text());
	$(".AutidTimes").html($(".ftimes").text());
	$(".AutidZyid").html($(".fzyid").text());
	$(".AutidCydate").html($(".fcydate").text());
	

}

//打开审核dialog
function openMSGAudit(){
	$("#auditTree li:eq(0)>div:eq(0)").addClass("tree-node-selected");   //设置第一个节点高亮   
	$("#messageDialog").dialog({
        closed: false,
        modal: true,
		title: "消息提示",
		buttons:[{
			text:"确认", handler:function(){ 
				//审核通过
				//遍历整个tree，获取所有图片的编码和路径地址传入后台	
				var node = $("#auditTree").tree("getSelected");
				var oneArr = [];
				oneArr = node.children;
				var path = ""; //存放路径
			//	var numbers = ""; //存放图片编码
				for (var i = 0; i < oneArr.length; i++) {
					var secondary = oneArr[i].children;
					for (var j = 0; j < secondary.length; j++) {
						if(secondary.length > 0){
							path +=  secondary[j].attributes + ",";
						}
					}
				}
			 
				//var path = $("#slider").find('li[class*=on]>div>.imgPath').text();//图片路径
				//var categoryNumber = $("#slider").find('li[class*=on]>div>.imgCategory').text();//图片分类编码
				//var number = $("#slider").find('li[class*=on]>div>.auditImgNumber').text();//图片编码
				$.ajax({
					url:ctx + "/MedicalShootManage/auditOperation",
					dataType:'json',
					type:'post',
					data:{
					//	"id" : id,
						"path" : path,
					//	"categoryNumber" : categoryNumber,
						"fprn" : $(".AutidPrn").text(),//病案号
						"fname" : $(".AutidName").text(), //姓名
						"ftimes" : $(".AutidTimes").text(),	//次数
						"fzyid" : $(".AutidZyid").text(),	//住院ID
						"barcode":$(".barcode").text()  //条形码
					},
					success:function(data){
						if(data.msg == true){
							alert("审核成功！");
							$("#messageDialog").dialog("close");
							$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点   
						}else{
							alert("审核操作失败,请联系管理员！");
							$("#messageDialog").dialog("close");
							$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点
						}
					},
					error:function(){
						alert("审核操作失败,请联系管理员！");
						$("#messageDialog").dialog("close");
						$("#auditTree li:eq(0)>div:eq(0)").removeClass("tree-node-selected");   //取消第一个节点高亮，显示最后点击的节点
					}
				});
		  }
		},{
			text:"关闭",
			handler:function(){
				$('#messageDialog').dialog("close");
			}
		}
		]
	});
	$(".AutidPrn").html($(".fprn").text());
	$(".AutidName").html($(".fname").text());
	$(".AutidTimes").html($(".ftimes").text());
	$(".AutidZyid").html($(".fzyid").text());
	$(".AutidCydate").html($(".fcydate").text());
	
};


//打开错误上交dialog
function openErrorAudit(){
	var node = $("#auditTree").tree("getSelected");
	if(!node){
		alert("请选中需要上报的图片");
		return;
	}
	$("#errorDialog").dialog({
        closed: false,
        modal: true,
		title: "错误上报",
		buttons:[{
			text:"确认", handler:function(){ 
				//var imgNumber = $("#slider .slide1_on>.caption>.auditImgNumber").text(); //图片编码
				//var category = $("#slider .slide1_on>.caption>.imgCategory").text(); //图片分类编码
				var imgNumber = $("#slider").find('li[class*=on]>div>.auditImgNumber').text();
				var category = $("#slider").find('li[class*=on]>div>.imgCategory').text();
				var text = $("#textArea").val();
				var reason = $("#reason").combobox("getText");
				$.ajax({
					url:ctx + "/MedicalShootManage/errorReport",
					dataType:'json',
					type:'post',
					data:{
						"fprn" : $(".fprn").text(),
						"fname" : $(".fname").text(),
						"ftimes" : $(".ftimes").text(),
						"fzyid" : $(".fzyid").text(),
						"imgNumber" : imgNumber,
						"category" : category,
						"text" : text,
						"reason" : reason,
						"barcode" : $(".barcode").text(),
						'pageCode' : $("#Page").numberbox("getValue")
					},
					success:function(data){
						if(data.msg == true){
							alert("上报成功！");
							$("#errorDialog").dialog("close");
						}else if(data.msg == "error"){
							alert("图片已经上报，请重新操作!");
							$("#errorDialog").dialog("close");
						}else{
							alert("上报操作失败，请联系管理员！");
							$("#errorDialog").dialog("close");
						}
					},
					error:function(){
						alert("上报操作失败，请联系管理员！");
						$("#errorDialog").dialog("close");
					}
					
				});
			}
		},{
			text:"关闭",
			handler:function(){
				$('#errorDialog').dialog("close");
			}
		}
		]
	});
	$("#auditFprn").html($(".fprn").text());
	$("#audit_fname").html($(".fname").text());
	$("#audit_fzyid").html($(".fzyid").text());
	$("#audit_ftimes").html($(".ftimes").text());
	$("#audit_fcydate").html($(".fcydate").text());
	var page = $("#slider").find('li[class*=on]>div>.pageCode').text(); //获取页码
	var str = page.substr(1,(page.length-1)); //拆分页码
	var pageCode = str.substr(0,(str.length-1));
	$("#Page").numberbox("setValue",pageCode);
	
}





